library(WDI)
library(sf)
library(tmap)
library(rnaturalearth)
library(rnaturalearthdata)
library(tidyverse)
library(grid)

1 World Map

country_boundaries<-ne_countries(scale="medium", returnclass="sf")
tm_shape(country_boundaries)+
  tm_polygons()

WDI_variables<-WDIsearch()
View(WDI_variables)
WDI_variables %>% datatable(extensions=c("Scroller", "FixedColumns"), options = list(
  deferRender = TRUE,
  scrollY = 350,
  scrollX = 350,
  dom = "t",
  scroller = TRUE,
  fixedColumns = list(leftColumns = 3)
))
trade_gdp_string<-"NE.TRD.GNFS.ZS"
trade_gdp_2010_2018<-WDI(country="all",
                                indicator=trade_gdp_string,
                                start=2010,
                                end=2018,
                                extra=T)
trade_gdp_2015<-trade_gdp_2010_2018 %>% 
                  filter(year=="2015")
View(trade_gdp_2015)
trade_2015_spatial<-full_join(country_boundaries, trade_gdp_2015,
                                    by=c("iso_a3"="iso3c"))
View(trade_2015_spatial)
## Warning in instance$preRenderHook(instance): It seems your data is too big
## for client-side DataTables. You may consider server-side processing: https://
## rstudio.github.io/DT/server.html
trade_2015_spatial<-trade_2015_spatial %>% 
                      rename(trade_pct_gdp=NE.TRD.GNFS.ZS)
tm_shape(trade_2015_spatial)+
  tm_polygons(col="trade_pct_gdp", palette="YlOrRd", style="jenks",
              n=5)
## Warning: The shape trade_2015_spatial contains empty units.

unique(trade_2015_spatial$subregion)
##  [1] "Caribbean"                 "Southern Asia"            
##  [3] "Middle Africa"             "Southern Europe"          
##  [5] "Northern Europe"           "Western Asia"             
##  [7] "South America"             "Polynesia"                
##  [9] "Antarctica"                "Australia and New Zealand"
## [11] "Seven seas (open ocean)"   "Western Europe"           
## [13] "Eastern Africa"            "Western Africa"           
## [15] "Eastern Europe"            "Central America"          
## [17] "Northern America"          "South-Eastern Asia"       
## [19] "Southern Africa"           "Eastern Asia"             
## [21] "Northern Africa"           "Melanesia"                
## [23] "Micronesia"                "Central Asia"             
## [25] NA
trade_2015_spatial_sea<-trade_2015_spatial %>% 
                            filter(subregion=="South-Eastern Asia")

y<-tm_shape(trade_2015_spatial_sea)+
  tm_polygons(col="trade_pct_gdp", palette="YlOrRd", style="jenks",
              n=5)

y

library(grid)
x<-tm_shape(trade_2015_spatial)+
  tm_polygons(col="trade_pct_gdp", palette="YlOrRd", style="jenks",
              n=5)
x
## Warning: The shape trade_2015_spatial contains empty units.
print(y, vp = viewport(0.8, 0.27, width = 0.5, height = 0.5))

library(grid)
x<-tm_shape(trade_2015_spatial)+
  tm_polygons(col="trade_pct_gdp", palette="YlOrRd", style="jenks",
              n=5)
x
## Warning: The shape trade_2015_spatial contains empty units.
print(y, vp = viewport(0.8, 0.18, width = 0.3, height = 0.3))
## Legend labels were too wide. The labels have been resized to 0.45, 0.45, 0.41, 0.38, 0.38. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

https://ecodiv.earth/post/creating-a-map-with-inset-using-tmap/

2 Country-Level Map

3 USA Local Map